package cn.endeavour.pay.service.impl;


import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.service.impl.BaseServiceImpl;
import cn.endeavour.order.service.IProductOrderService;
import cn.endeavour.pay.domain.AlipayInfo;
import cn.endeavour.pay.domain.PayBill;
import cn.endeavour.pay.mapper.AlipayInfoMapper;
import cn.endeavour.pay.mapper.PayBillMapper;
import cn.endeavour.pay.service.IPayBillService;
import cn.endeavour.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String payRequest(PayBill bill) {
        // 先判断payBill是否为null
        if (bill == null) {
            throw new BusinessException("请确认您的订单!!!");
        }
        // 查看订单是否存在
        PayBill loadByUnionPaySn = payBillMapper.loadByUnionPaySn(bill.getUnionPaySn());
        if (loadByUnionPaySn == null) {
            throw new BusinessException("请确认您的订单!!!");
        }
        switch (bill.getPayChannel().intValue()) {
            case 1 :
                //支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(bill, info);
            //微信
            case 2 :
                //@TODO
                break;
            //银联
            case 3 :
                //@TODO
                break;
            //余额
            default :
                //@TODO
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
